package com.base.cn.platform.os.controller.op.activity;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.service.manage.op.activity.OpActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * 促销活动
 * @author ll
 * @since 2018/5/10
 */
@Controller
@RequestMapping("/admin/op/activity")
public class AdminOpActivityController extends AdminBaseController {

    @Autowired
    private OpActivityService opActivityService;


    /**
     * 分页搜索促销活动
     * @param opActivity 活动
     * @param currentPage 当前页
     * @return
     */
    @RequestMapping("/queryOpActivityListPage")
    public String queryOpActivityListPage(Model model,@RequestBody Map<String, Object>  opActivity,
                                          @RequestParam(value = "currentPage",required = false,defaultValue = "1")Integer currentPage){
        Map<String,Object> dataMap =opActivityService.queryOpActivityListPage(opActivity,currentPage,true,true,true,true);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/op/activity/ajax_activity_list");
    }


    /**
     * 分页搜索促销活动
     * @return
     */
    @RequestMapping("/toQueryOpActivityList")
    public String  toQueryOpActivityList(){
        return resultPage("/op/activity/list_activity");
    }


    /**
     * 修改活动状态
     * @param activityIds
     * @param status
     * @return
     */
    @RequestMapping("/updateActivityStatusByIds")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "修改活动状态")
    public Map<String,Object> updateActivityStatusByIds(@RequestParam("activityIds")String activityIds,
                                                        @RequestParam("status")Integer status){
        return opActivityService.updateActivityStatusByIds(activityIds,status);
    }


    /**
     * 发布促销活动
     * @param activityIds
     * @param releaseStatus
     * @return
     */
    @RequestMapping("/updateActivityReleaseStatusByIds")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "发布活动")
    public Map<String,Object> updateActivityReleaseStatusByIds(@RequestParam("activityIds")String activityIds,
                                                               @RequestParam("releaseStatus")Integer releaseStatus){
        return opActivityService.updateActivityReleaseStatusByIds(activityIds,releaseStatus);
    }


    /**
     * 添加活动页面
     * @return
     */
    @RequestMapping("/toSaveUpdateOpActivityPage")
    public String  toSaveUpdateOpActivityPage(){
        return resultPage("/op/activity/save_activity");
    }


    /**
     * 活动详情页面
     * @return
     */
    @RequestMapping("/opActivityInfoById")
    public String  opActivityInfoById(Model model,
                                      @RequestParam(value = "activityId",required = false,defaultValue = "0")BigDecimal activityId){
        if(DataUtil.idIsNotNull(activityId)){
            Map<String,Object> opActivity = opActivityService.queryOpActivityById(activityId,true,true,true,true);
            model.addAttribute("activity",opActivity);
        }
        return resultPage("/op/activity/activity_info");
    }

    /**
     * 添加修改促销活动
     * @param opActivity
     * @return
     */
    @RequestMapping("/addOrUpdateActivity")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "添加或修改活动")
    public Map<String, Object> addOrUpdateActivity(HttpServletRequest request,
                                                   @RequestBody Map<String, Object> opActivity){
        BigDecimal createUserId = loginUtil.getLoginUserId(request);
        opActivity.put("createUserId",createUserId);
        opActivity.put("activityType_1",null);
        opActivity.put("giftType_1",null);
        return opActivityService.toAddOrUpdateActivity(opActivity);
    }





}
